<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="会员名称" prop="mebName">
        <el-input v-model="dataForm.mebName" placeholder="会员名称" @click.native="memberHandle()"></el-input>
      </el-form-item>
      <el-form-item label="就餐方式" prop="eatNameFront">
        <el-input v-model="dataForm.eatNameFront" placeholder="就餐类型" @click.native="resHandle()"></el-input>
      </el-form-item>
      <el-form-item label="总价" prop="price">
        <el-input v-model="dataForm.price" placeholder="总价">
          <template slot="append">￥</template>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
    <member-visible v-if="memberVisible" ref="member" @memberCallback="memberCallback"/>
    <res-discount v-if="resVisible" ref="res" @resCallback="resCallback"/>
  </el-dialog>
</template>
<script>
  import memberVisible from '@/components/member'
  import resDiscount from '@/components/res-discount'

  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        memberVisible: false,
        resVisible: false,
        dataForm: {
          id: 0,
          disId: '',
          resName: '',
          eatNameFront: '',
          eatName: '',
          mebId: '',
          mebName: '',
          price: '',
          createTime: '',
          isDel: ''
        },
        dataRule: {
          mebName: [
            {required: true, message: '会员不能为空', trigger: 'blur'}
          ],
          eatNameFront: [
            {required: true, message: '就餐类型不能为空', trigger: 'blur'}
          ],
          price: [
            {required: true, message: '价格不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: false, message: '创建时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
      memberVisible,
      resDiscount
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/resrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.disId = data.resRecord.disId
                this.dataForm.mebId = data.resRecord.mebId
                this.dataForm.mebName = data.resRecord.mebName
                this.dataForm.resName = data.resRecord.resName
                this.dataForm.eatName = data.resRecord.eatName
                this.dataForm.eatNameFront = data.resRecord.resName + '/' + data.resRecord.eatName
                this.dataForm.price = data.resRecord.price
                this.dataForm.createTime = data.resRecord.createTime
                this.dataForm.isDel = data.resRecord.isDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/resrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'disId': this.dataForm.disId,
                'resName': this.dataForm.resName,
                'eatName': this.dataForm.eatName,
                'mebId': this.dataForm.mebId,
                'mebName': this.dataForm.mebName,
                'price': this.dataForm.price,
                'createTime': this.dataForm.createTime,
                'isDel': this.dataForm.isDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      },
      memberHandle () {
        this.memberVisible = true
        this.$nextTick(() => {
          this.$refs.member.init()
        })
      },
      memberCallback (row) {
        this.dataForm.mebId = row.id
        this.dataForm.mebName = row.name
      },
      resHandle () {
        this.resVisible = true
        this.$nextTick(() => {
          this.$refs.res.init()
        })
      },
      resCallback (row) {
        this.dataForm.resName = row.fatherName
        this.dataForm.eatName = row.eatName
        this.dataForm.eatNameFront = row.fatherName + '/' + row.eatName
        this.dataForm.disId = row.id
      },
      // 二维码核验传值入住
      open (memberStr) {
        this.dataForm.id = 0
        this.visible = true
        this.isLoading = false
        let mebJSON = JSON.parse(memberStr)
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.mebName = mebJSON.name
          this.dataForm.mebId = mebJSON.id
        })
      }
    }
  }
</script>
